A = ""
B = ""
C = 0
ANS = 0
DEC = 0
N = 1
W_WIDE = 118
W_TALL = 240
TEMP = ""
AboutIsOpen = false
HistoryIsOpen = false
function Calculator()
    Frame = vgui.Create( "DFrame" )   //MAIN FRAME
        Frame:SetSize( 118, 260 )
        Frame:SetPos( 10, 10 )
        Frame:SetVisible( true )
        Frame:SetTitle( "G-Calc 2.2" )
        Frame:ShowCloseButton( false )
        Frame:MakePopup()
		      Frame.Paint = function()
				        draw.RoundedBox( 6, 0, 0, 118, 260, Color(50,50,50,200) )
            draw.RoundedBox( 6, 1, 1, 116, 20, Color( 100, 50, 50, 200 ) )
					       draw.RoundedBox( 4, 87, 30, 21, 115, Color(130, 130, 130, 255))
					       draw.RoundedBox( 4, 10, 124, 76, 21, Color(130, 130, 130, 255))
					       draw.RoundedBox( 4, 10, 146, 98, 65, Color(0, 255, 0, 100))
				    end
        local Close = vgui.Create( "DButton", Frame )
            Close:SetPos( 10, 235 )
            Close:SetSize( 98, 20 )
            Close:SetText( "X Close X" )
            Close.DoClick = function()
                if AboutIsOpen == true then
                    W_About:Close()
                end
                if HistoryIsOpen == true then
                    W_History:Close()
                end
                Frame:Close()
            end
        local About = vgui.Create( "DButton", Frame )
            About:SetSize( 68, 17 )
            About:SetText( "?" )
            About:SetPos( 14, 126)
            About:SetToolTip("about calc")
            About.DoClick = function()
                if AboutIsOpen == false then
                    W_About:SetVisible( true ); AboutIsOpen = true
                else
                    W_About:SetVisible( false ); AboutIsOpen = false
                end
            end
    local History = vgui.Create( "DButton", Frame ) // History Popup button
        History:SetSize( 98, 20 )
        History:SetPos( 10, 215 )
        History:SetText( "History" )
        History.DoClick = function()
            if HistoryIsOpen == false then
                W_History:SetVisible( true ); HistoryIsOpen = true
            else
                W_History:SetVisible( false ); HistoryIsOpen = false
            end
        end
     

    W_About = vgui.Create( "DFrame" ) // CREATE ABOUT FRAME
        W_About:SetSize( 200, 100 )
        W_About:Center()
        W_About:SetVisible( false )
        W_About:SetDraggable( false )
        W_About:SetTitle( "About G-Calc" )
        W_About:ShowCloseButton( false )
        W_About:MakePopup()
        W_About.Paint = function()
            draw.RoundedBox( 6, 0, 0, 200, 100, Color( 50, 50, 50, 200 ) )
            draw.RoundedBox( 6, 1, 1, 198, 20, Color( 100, 50, 50, 200 ) )
            draw.SimpleText("G-Calc 2.2", "ScoreboardText", 10, 30, Color( 255, 0, 0, 200 ), TEXT_ALLIGN_CENTER )
            draw.SimpleText(" Written by: T_larson911", "ScoreboardText", 10, 50, Color( 255, 0, 0, 200 ), TEXT_ALLIGN_CENTER )
            draw.SimpleText(" Release: Dec 15, 2010", "ScoreboardText", 10, 70, Color( 255, 0, 0, 200 ), TEXT_ALLIGN_CENTER )
        end

    W_History = vgui.Create( "DFrame" ) // CREATE HISTORY WINDOW
        W_History:SetSize( 300, 220 )
        W_History:SetPos( 200, 10 )
        W_History:SetVisible( false )
        W_History:SetTitle( "Calculator History" )
        W_History:ShowCloseButton( false)
        W_History:SetDraggable( true )
        W_History:MakePopup()
        W_History.Paint = function()
            draw.RoundedBox( 6, 0, 0, 300, 200, Color( 50, 50, 50, 200 ) )
            draw.RoundedBox( 6, 1, 1, 298, 20, Color( 100, 50, 50, 200 ) )
        end
        local HistoryList = vgui.Create( "DListView", W_History ) //Create History List, to History Window
            HistoryList:SetPos( 10, 30 )
            HistoryList:SetSize( 280, 140 )
            HistoryList:AddColumn( "History" )

        local HistoryClear = vgui.Create( "DButton", W_History )
            HistoryClear:SetPos( 10, 174 )
            HistoryClear:SetSize( 280, 20 )
            HistoryClear:SetText( "Clear History List" )
            HistoryClear.DoClick = function()
                HistoryList:Clear()
            end
        
    local Number1 = vgui.Create( "DLabel", Frame )
        Number1:SetPos( 25, 148 )
        Number1:SetText("0") 
    local Number2 = vgui.Create( "DLabel", Frame )
        Number2:SetPos( 25, 168 )
        Number2:SetText("0")
    local Number3 = vgui.Create( "DLabel", Frame )
        Number3:SetPos( 25, 188 )
        Number3:SetText("0")
    local Op = vgui.Create( "DLabel", Frame )
        Op:SetPos( 15, 168 )
        Op:SetText( "" )
    local Eq = vgui.Create( "DLabel", Frame )
        Eq:SetPos( 15, 188 )
        Eq:SetText( "=" )
    local NumPad = vgui.Create( "DNumPad", Frame )
        NumPad:SetPos( 10, 30 )
    local Clear = vgui.Create( "DButton", Frame )
        Clear:SetPos( 14, 34 )
        Clear:SetSize( 17, 17 )
        Clear:SetText( "c" )
    local Square2 = vgui.Create( "DButton", Frame )
        Square2:SetPos( 89, 51 )
        Square2:SetSize( 17, 17 )
        Square2:SetText("x2")
        Square2.DoClick = function()
            if N == 1 then
                A = ""..tonumber(A^2)
                Number1:SetText(A)
            else
                B = ""..tonumber(B^2)
                Number2:SetText(B)
            end
        end
    local Square = vgui.Create( "DButton", Frame )
        Square:SetPos( 89, 34 )
        Square:SetSize( 17, 17 )
        Square:SetText("^")
        Square.DoClick = function()
			if ANS == 1 then
                Number1:SetText(Number3:GetValue())
                B = "0"
                Number2:SetText(B)
				ANS = 0
				Number3:SetText("0")
            end
                NumPad:SetSelected(-1)
                DEC = 0
                Op:SetText("^")
                N = 2
        end
        NumPad.OnButtonPressed = function()
            if NumPad:GetValue()== 10 then
                if DEC == 0 then
                    if N == 1 then
                        if A == "0" then
                            A = A.."."
                        else
                            A = A.."."
                        end
                        Number1:SetText(A)
                    else
                        if B == "0" then
                            B = B.."."
                        else
                            B = B.."."
                        end
                        Number2:SetText(B)
                    end
                    DEC = 1
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue()== 12 then
                if ANS == 1 then
                    Number1:SetText(Number3:GetValue())
                    B = "0"
                    Number2:SetText(B)
					ANS = 0
					Number3:SetText("0")
                else
                end
                NumPad:SetSelected(-1)
                DEC = 0
                Op:SetText("+")
                N = 2
            end
            if NumPad:GetValue()== 13 then
                if ANS == 1 then
                    Number1:SetText(Number3:GetValue())
                    B = "0"
                    Number2:SetText(B)
					ANS = 0
					Number3:SetText("0")
                else
                end
                NumPad:SetSelected(-1)
                DEC = 0
                Op:SetText("-")
                N = 2
            end
            if NumPad:GetValue()== 15 then
                if ANS == 1 then
                    Number1:SetText(Number3:GetValue())
                    B = "0"
                    Number2:SetText(B)
					ANS = 0
					Number3:SetText("0")
                else
                end
                NumPad:SetSelected(-1)
                DEC = 0
                Op:SetText("/")
                N = 2
            end
            if NumPad:GetValue()== 14 then
                if ANS == 1 then
                    Number1:SetText(Number3:GetValue())
                    B = "0"
                    Number2:SetText(B)
					ANS = 0
					Number3:SetText("0")
                else
                end
                NumPad:SetSelected(-1)
                DEC = 0
                Op:SetText("*")
                N = 2
            end
            if NumPad:GetValue() == 1 then  
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."1"; Number1:SetText(tonumber(A))
                else
                    B = B.."1"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 2 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."2"; Number1:SetText(tonumber(A))
                else
                    B = B.."2"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 3 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."3"; Number1:SetText(tonumber(A))
                else
                    B = B.."3"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 4 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."4"; Number1:SetText(tonumber(A))
                else
                    B = B.."4"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 5 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."5"; Number1:SetText(tonumber(A))
                else
                    B = B.."5"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 6 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."6"; Number1:SetText(tonumber(A))
                else
                    B = B.."6"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 7 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."7"; Number1:SetText(tonumber(A))
                else
                    B = B.."7"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 8 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."8"; Number1:SetText(tonumber(A))
                else
                    B = B.."8"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 9 then
                if ANS == 1 then
                    N = 1; A = ""; B = ""; C = 0; ANS = 0
                    Op:SetText( "" ); Number1:SetText("0"); Number2:SetText("0"); Number3:SetText(C)   
                end  
                if N == 1 then
                    A = A.."9"; Number1:SetText(tonumber(A))
                else
                    B = B.."9"; Number2:SetText(tonumber(B))
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 0 then
                if N == 1 then
                    if tonumber(A) >= 1 then
                        A = A.."0"; Number1:SetText(tonumber(A))
                    end
                else
                    if tonumber(B) >= 1 then
                        B = B.."0"; Number2:SetText(tonumber(B))
                    end
                end
                NumPad:SetSelected(-1)
            end
            if NumPad:GetValue() == 11 then
                if Op:GetValue() == "+" then
                    HistoryList:AddLine(tonumber(A).." + "..tonumber(B).." = "..tonumber(Number1:GetValue()) + tonumber(Number2:GetValue()))
                    print(tonumber(A).." + "..tonumber(B).." = "..tonumber(Number1:GetValue()) + tonumber(Number2:GetValue()))
                    Number3:SetText(tonumber(Number1:GetValue()) + tonumber(Number2:GetValue()))
                end
                if Op:GetValue() == "-" then
					               HistoryList:AddLine(tonumber(A).." - "..tonumber(B).." = "..tonumber(Number1:GetValue()) - tonumber(Number2:GetValue()))
                    print(tonumber(A).." + "..tonumber(B).." = "..tonumber(Number1:GetValue()) - tonumber(Number2:GetValue()))
                    Number3:SetText(tonumber(Number1:GetValue()) - tonumber(Number2:GetValue()))
                end
                if Op:GetValue() == "*" then
					               HistoryList:AddLine(tonumber(A).." * "..tonumber(B).." = "..tonumber(Number1:GetValue()) * tonumber(Number2:GetValue()))
                    print(tonumber(A).." + "..tonumber(B).." = "..tonumber(Number1:GetValue()) * tonumber(Number2:GetValue()))
                    Number3:SetText(tonumber(Number1:GetValue()) * tonumber(Number2:GetValue()))
                end
                if Op:GetValue() == "/" then
					               HistoryList:AddLine(tonumber(A).." / "..tonumber(B).." = "..tonumber(Number1:GetValue()) / tonumber(Number2:GetValue()))
                    print(tonumber(A).." + "..tonumber(B).." = "..tonumber(Number1:GetValue()) / tonumber(Number2:GetValue()))
                    Number3:SetText(tonumber(Number1:GetValue()) / tonumber(Number2:GetValue()))
                end
                if Op:GetValue() == "^" then
					               HistoryList:AddLine(tonumber(A).." ^ "..tonumber(B).." = "..tonumber(Number1:GetValue()) ^ tonumber(Number2:GetValue()))
                    print(tonumber(A).." + "..tonumber(B).." = "..tonumber(Number1:GetValue()) ^ tonumber(Number2:GetValue()))
                    Number3:SetText(tonumber(Number1:GetValue()) ^ tonumber(Number2:GetValue()))
                end
                NumPad:SetSelected(-1)
                ANS = 1
            end
        end
        Clear.DoClick = function()
            N = 1
            A = 0
            B = 0
            C = 0
            ANS = 0
            Op:SetText( "" )
            Number1:SetText(A); Number2:SetText(B); Number3:SetText(C)
        end
        
    
end

concommand.Add("calc", Calculator)
concommand.Add("calculator", Calculator)
